<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <!-- 通过iconfont添加左上角X按钮 -->
      <!-- <i slot="left" class="iconfont icon-guanbi"></i> -->
      <!-- 通过vant组件库内置的icon图标 -->
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
    <van-field
      v-model="user.mobile"
      name="mobile"
      placeholder="请输入手机号"
      :rules= userFormRules.mobile
      type="number"
      maxlength="11"
    >
    <i slot="left-icon" class="iconfont icon-shouji"></i>
  </van-field>
    <van-field
      v-model="user.code"
      name="code"
      placeholder="请输入验证码"
      :rules= userFormRules.code
      type="number"
      maxlength="6"
    >
    <i slot="left-icon" class="iconfont icon-yanzhengma" ></i>
    <template #button>
      <van-count-down v-if="isCountDownShow" :time="1000*60" format="ss 秒" @finish="isCountDownShow=false" />
    <van-button v-else native-type="button" round size="mini" type="default" class="send-sms-btn" @click="onSendSms">发送验证码</van-button>
  </template>
  </van-field>
  <div class="login-btn-wrap">
    <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import { login } from "@/api/user"
export default {
  name: "LoginIndex",
  methods: {
    async onSubmit() {
      // 获取表单数据
      const user = this.user
      // 表单验证

      // 登录状态提示
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 3000
      });
      // 提交表单请求登录
      try {
        const res = await login(user)
        // 存储token
        this.$store.commit('setUser',res.data.data)
        this.$toast.success("登录成功")
        console.log("登录成功", res);
        // 登录成功，从哪来回哪去，此处并不严谨，后面再优化
        this.$router.back()
      } catch (err) {
        if (err.request.status === 400) {
          this.$toast.fail("手机号或验证码错误")
          // console.log("手机号或验证码错误");
        } else {
          this.$toast.fail("登录失败，请稍后重试")
          // console.log("登录失败，请稍后重试",err);
        }
      }
    },
    async onSendSms() {
      // 验证手机号
      // 通过$refs获取到组件实例再调用validate方法，返回的是promise，所以用async await
      try {
        console.log("验证成功");
      await this.$refs.loginForm.validate('mobile'); 
      } catch (err) {
        // 此处的return 是验证失败 代码不再往后执行
        return console.log("验证失败",err);
      }
      this.isCountDownShow = true
    }

  },
  data() {
    return {
      user: {
        mobile: '',
        code:''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8]\d{9}$/ ,message:"手机号格式错误" }
        ],
        code: [
          { required: true, message: "验证码不能为空" },
          { pattern: /^\d{6}$/ ,message:"验证码格式错误"}
        ]
      },
      isCountDownShow:false,
    }
  }
}
</script>

<style scoped lang="less">
.iconfont{
  font-size: 28px;
}
.send-sms-btn{
  color: rgba(7, 7, 7, 0.4);
  background-color: #ececec;
}
.login-btn-wrap{
  padding: 30px 20px;
  .login-btn{
    border-radius: 5px;
  }
}
</style>